#include "pwm.h"
// static char pwm_path_tim3_ch2[] = "/sys/class/pwm/pwmchip0/pwm1";
// static char pwm_path_tim4_ch2[] = "/sys/class/pwm/pwmchip4/pwm1";
// static char pwm_path_tim5_ch2[] = "/sys/class/pwm/pwmchip8/pwm1";

/* 打开pwm通道, eg:3  4   5    tim3_ch2  tim4_ch2  tim5_ch2*/
int open_pwm(int tim_ch)
{   
    int fd = -1;
    if(tim_ch == 3) {
        fd = open("/sys/class/pwm/pwmchip0/export", O_WRONLY);
    } else if(tim_ch == 4) {
        fd = open("/sys/class/pwm/pwmchip4/export", O_WRONLY);
    } else if(tim_ch == 5) {
        fd = open("/sys/class/pwm/pwmchip8/export", O_WRONLY);
    }
    if(fd < 0) {
        perror("open open_pwm error");
        exit(-1);
    }
    if(1 != write(fd, "1", 1)) {
        printf("%s fd = %d\n",__func__,fd);
        perror("open_pwm write error");
        close(fd);
        exit(-1);
    }
    close(fd);
    return 0;
}

/* 关闭pwm通道, eg:3  4   5    tim3_ch2  tim4_ch2  tim5_ch2*/
int close_pwm(int tim_ch)
{   
    int fd = -1;
    if(tim_ch == 3) {
        fd = open("/sys/class/pwm/pwmchip0/unexport", O_WRONLY);
    } else if(tim_ch == 4) {
        fd = open("/sys/class/pwm/pwmchip4/unexport", O_WRONLY);
    } else if(tim_ch == 5) {
        fd = open("/sys/class/pwm/pwmchip8/unexport", O_WRONLY);
    }
    if(fd < 0) {
        perror("open close_pwm error");
        exit(-1);
    }
    if(1 != write(fd, "1", 1)) {
        perror("open_close write error");
        close(fd);
        exit(-1);
    }
    close(fd);
    return 0;
}

/* 
* 配置pwm 
* 配置周期   5000ns pwm_config(LED, "period", "5000")
* 配置占空比 3000ns pwm_config(LED, "duty_cycle", "3000")
* 启动PWM   pwm_config(DENG, "enable", "1");
* 停止PWM   pwm_config(DENG, "enable", "0");
*/
int pwm_config(int tim_ch, const char *attr, const char *val)
{   
    int fd = -1;
    int len = 0;
    char file_path[100];
    if(tim_ch == 3) 
        sprintf(file_path, "/sys/class/pwm/pwmchip0/pwm1/%s", attr);
    else if(tim_ch == 4)
        sprintf(file_path, "/sys/class/pwm/pwmchip4/pwm1/%s", attr);
    else if(tim_ch == 5)
        sprintf(file_path, "/sys/class/pwm/pwmchip8/pwm1/%s", attr);
    fd = open(file_path, O_WRONLY);
    if(fd < 0) {
        perror("open pwm_config");
        return fd;
    }
    len = strlen(val);
    if(len != write(fd, val, len)) {
        perror("pwm_config writer error");
        close(fd);
        return -1;
    }
    close(fd);
    return 0;
}

void led_control(int flag)
{   
    if(flag == 1) {             //开灯
        open_pwm(LED);
        pwm_config(LED, "period", "1000000");
        pwm_config(LED, "duty_cycle", "300000");
        pwm_config(LED, "enable", "1");
        close_pwm(LED);
    } else if(flag == 2) {     //关灯 
        open_pwm(LED);
        pwm_config(LED, "enable", "0");
        close_pwm(LED);
    } else if(flag == 3) {      //把灯调亮
        open_pwm(LED);
        pwm_config(LED, "period", "1000000");
        pwm_config(LED, "duty_cycle", "1000000");
        pwm_config(LED, "enable", "1");
        close_pwm(LED);
    } else if(flag == 4) {      //把灯调暗
        open_pwm(LED);
        pwm_config(LED, "period", "1000000");
        pwm_config(LED, "duty_cycle", "200000");
        pwm_config(LED, "enable", "1");
        close_pwm(LED);
    }
}

void tuo_control(int flag)
{   
    open_pwm(TUO);
    pwm_config(TUO, "period", "20000000");
    switch(flag) {
        case 4:
            pwm_config(TUO, "duty_cycle", "1800000");
            break;
        case 5:
            pwm_config(TUO, "duty_cycle", "1600000");
            break;
        case 6:
            pwm_config(TUO, "duty_cycle", "1400000");
            break;
        case 7:
            pwm_config(TUO, "duty_cycle", "1200000");
            break;
    }
    pwm_config(TUO, "enable", "1");
    close_pwm(TUO);
}

void beng_control(int flag, int freq)
{   
    char freq_data[20];
    char duty_data[20];
    int duty = freq * 0.5;
    if(flag == 8) {
        open_pwm(BENG);
        sprintf(freq_data,"%d",freq);
        sprintf(duty_data,"%d", duty);
        //printf("freqdata=%s dutydata=%s \n",freq_data,duty_data);
        pwm_config(BENG, "period", freq_data);
        pwm_config(BENG, "duty_cycle", duty_data);
        pwm_config(BENG, "enable", "1");
        close_pwm(BENG);
    } else if(flag == 9) {
        open_pwm(BENG);
        pwm_config(BENG, "enable", "0");
        close_pwm(BENG);
    }
}
